package cn.yudao.ymcc.web.controller;

import cn.yudao.ymcc.service.IUserCodeService;
import cn.yudao.ymcc.domain.UserCode;
import cn.yudao.ymcc.query.UserCodeQuery;
import cn.yudao.ymcc.result.JSONResult;
import cn.yudao.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userCode")
public class UserCodeController {

    @Autowired
    public IUserCodeService userCodeService;

    /**
     * 删除对象
     */
    @RequestMapping(value="/loadCode",method=RequestMethod.GET)
    public JSONResult loadCode(){
        String code = userCodeService.loadCode();
        return JSONResult.success(code);
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody UserCode userCode){
        if(userCode.getId()!=null){
            userCodeService.updateById(userCode);
        }else{
            userCodeService.insert(userCode);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        userCodeService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(userCodeService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(userCodeService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody UserCodeQuery query){
        Page<UserCode> page = new Page<UserCode>(query.getPage(),query.getRows());
        page = userCodeService.selectPage(page);
        return JSONResult.success(new PageList<UserCode>(page.getTotal(),page.getRecords()));
    }
}
